
package com.rabbits.tool.storage;

import android.text.TextUtils;

import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;

public class RNStorageModule extends ReactContextBaseJavaModule {

    private final ReactApplicationContext reactContext;

    public RNStorageModule(ReactApplicationContext reactContext) {
        super(reactContext);
        this.reactContext = reactContext;
    }

    @Override
    public String getName() {
        return "RNStorage";
    }

    @ReactMethod
    public void clear() {
        StorageSingleton.getInstance(reactContext).clear();
    }

    @ReactMethod
    public void removeCache(String name) {
        StorageSingleton.getInstance(reactContext).removeCache(name);
    }

    @ReactMethod
    public void commit() {
        StorageSingleton.getInstance(reactContext).commit();
    }



    @ReactMethod
    public void putString(String name, String value) {
        if (!TextUtils.isEmpty(name)) {
            StorageSingleton.getInstance(reactContext).putString(name, value);
        }
    }
    @ReactMethod
    public void putStringCommit(String name, String value) {
        if (!TextUtils.isEmpty(name)) {
            StorageSingleton.getInstance(reactContext).putStringWithCommit(name, value);
        }
    }
    @ReactMethod
    public void getString(String name, String defaultValue, Promise promise) {
        if (!TextUtils.isEmpty(name)) {
            String result = StorageSingleton
                    .getInstance(reactContext).getString(name, defaultValue);
            promise.resolve(result);
        } else {
            promise.reject(new Throwable("name is null."));
        }
    }



    @ReactMethod
    public void putBool(String name, boolean value) {
        if (!TextUtils.isEmpty(name)) {
            StorageSingleton.getInstance(reactContext).putBoolean(name, value);
        }
    }
    @ReactMethod
    public void putBoolCommit(String name, boolean value) {
        if (!TextUtils.isEmpty(name)) {
            StorageSingleton.getInstance(reactContext).putBooleanWithCommit(name, value);
        }
    }
    @ReactMethod
    public void getBool(String name, boolean defaultValue, Promise promise) {
        if (!TextUtils.isEmpty(name)) {
            boolean result = StorageSingleton
                    .getInstance(reactContext).getBoolean(name, defaultValue);
            promise.resolve(result);
        } else {
            promise.reject(new Throwable("name is null."));
        }
    }




    @ReactMethod
    public void putInt(String name, int value) {
        if (!TextUtils.isEmpty(name)) {
            StorageSingleton.getInstance(reactContext).putInt(name, value);
        }
    }
    @ReactMethod
    public void putIntCommit(String name, int value) {
        if (!TextUtils.isEmpty(name)) {
            StorageSingleton.getInstance(reactContext).putIntWithCommit(name, value);
        }
    }
    @ReactMethod
    public void getInt(String name, int defaultValue, Promise promise) {
        if (!TextUtils.isEmpty(name)) {
            int result = StorageSingleton
                    .getInstance(reactContext).getInt(name, defaultValue);
            promise.resolve(result);
        } else {
            promise.reject(new Throwable("name is null."));
        }
    }





    @ReactMethod
    public void putLong(String name, long value) {
        if (!TextUtils.isEmpty(name)) {
            StorageSingleton.getInstance(reactContext).putLong(name, value);
        }
    }
    @ReactMethod
    public void putLongCommit(String name, long value) {
        if (!TextUtils.isEmpty(name)) {
            StorageSingleton.getInstance(reactContext).putLongWithCommit(name, value);
        }
    }
    @ReactMethod
    public void getLong(String name, long defaultValue, Promise promise) {
        if (!TextUtils.isEmpty(name)) {
            long result = StorageSingleton
                    .getInstance(reactContext).getLong(name, defaultValue);
            promise.resolve(result);
        } else {
            promise.reject(new Throwable("name is null."));
        }
    }






    @ReactMethod
    public void putFloat(String name, float value) {
        if (!TextUtils.isEmpty(name)) {
            StorageSingleton.getInstance(reactContext).putFloat(name, value);
        }
    }
    @ReactMethod
    public void putFloatCommit(String name, float value) {
        if (!TextUtils.isEmpty(name)) {
            StorageSingleton.getInstance(reactContext).putFloatWithCommit(name, value);
        }
    }
    @ReactMethod
    public void getFloat(String name, float defaultValue, Promise promise) {
        if (!TextUtils.isEmpty(name)) {
            float result = StorageSingleton
                    .getInstance(reactContext).getFloat(name, defaultValue);
            promise.resolve(result);
        } else {
            promise.reject(new Throwable("name is null."));
        }
    }

}